<?php
	namespace ampleharvest\task;

	require_once dirname(dirname(dirname(__FILE__))) . "/abstract_classes/abs_Task.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/Tables/cTABLE_Metric.php";
	require_once dirname(dirname(dirname(__FILE__))) . "/classes/search/solr/cSolr.php";

	class count_SOLR_Docs extends \abs_Task
	{
		const C_STR_NAME = "count_SOLR_Docs";

		public function __construct($str_LockPath)
		{
			$this->set_LockPath($str_LockPath);
			$this->set_Name(self::C_STR_NAME);
			parent::__construct();
		}

		public function run($bool_Force = false)
		{
			$this->set_Force($bool_Force);
			if (!parent::run())
			{
				return false;
			}

			$int_CurrentCount = $this->collect_Count();

			$this->store_Hourly($int_CurrentCount);
			$this->store_Daily($int_CurrentCount);
			$this->store_Monthly($int_CurrentCount);
			$this->store_Yearly($int_CurrentCount);
			return true;
		}

		private function store_Hourly($int_Count, $bool_Force = false)
		{
			$obj_StartTime = new \DateTime();
			$obj_StartTime->setTime($obj_StartTime->format("H"), 0, 0);

			$obj_EndTime = clone $obj_StartTime;
			$obj_EndTime->modify('+1 hours');

			$obj_TABLE_Metric = new \cTABLE_Metric();

			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Category, \cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Subcategory, \cTABLE_Metric::C_STR_SUBCATEGORY_HOURLY);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Group, \cTABLE_Metric::C_STR_GROUP_SOLR);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_StartTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_GREATER);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_EndTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_LESS);

			$int_Return = $obj_TABLE_Metric->do_Search();

			if (($int_Return < 1) || ($bool_Force))
			{
				$obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
				$obj_TABLE_Metric->set_subcategory(\cTABLE_Metric::C_STR_SUBCATEGORY_HOURLY);
				$obj_TABLE_Metric->set_group(\cTABLE_Metric::C_STR_GROUP_SOLR);
				$obj_TABLE_Metric->set_value($int_Count);

				$obj_TABLE_Metric->save_Data2DB();
			}
		}

		private function store_Daily($int_Count, $bool_Force = false)
		{
			$obj_StartTime = new \DateTime();
			$obj_StartTime->setTime(0, 0, 0);

			$obj_EndTime = clone $obj_StartTime;
			$obj_EndTime->modify('+1 days');

			$obj_TABLE_Metric = new \cTABLE_Metric();

			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Category, \cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Subcategory, \cTABLE_Metric::C_STR_SUBCATEGORY_DAILY);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Group, \cTABLE_Metric::C_STR_GROUP_SOLR);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_StartTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_GREATER);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_EndTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_LESS);

			$int_Return = $obj_TABLE_Metric->do_Search();

			$obj_TABLE_Metric->clear();

			if ($obj_TABLE_Metric->do_Search() > 0)
			{
				$arr_Results = $obj_TABLE_Metric->get_ResultSet();
				if (isset($arr_Results[0]))
				{
					$obj_TABLE_Metric->load_DataFromArray($arr_Results[0]);
					$obj_Date = new \DateTime();
					$obj_TABLE_Metric->set_datetime($obj_Date->format("Y-m-d H:i:s"));
				}
			}

			$obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->set_subcategory(\cTABLE_Metric::C_STR_SUBCATEGORY_DAILY);
			$obj_TABLE_Metric->set_group(\cTABLE_Metric::C_STR_GROUP_SOLR);
			$obj_TABLE_Metric->set_value($int_Count);

			$obj_TABLE_Metric->save_Data2DB();

		}

		private function store_Monthly($int_Count, $bool_Force = false)
		{
			$obj_StartTime = new \DateTime();
			$obj_StartTime->setTime(0, 0, 0);
			$obj_StartTime->setDate($obj_StartTime->format("Y"), $obj_StartTime->format("m"), 1);

			$obj_EndTime = clone $obj_StartTime;
			$obj_EndTime->modify('+1 months');

			$obj_TABLE_Metric = new \cTABLE_Metric();

			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Category, \cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Subcategory, \cTABLE_Metric::C_STR_SUBCATEGORY_MONTHLY);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Group, \cTABLE_Metric::C_STR_GROUP_SOLR);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_StartTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_GREATER);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_EndTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_LESS);

			if ($obj_TABLE_Metric->do_Search() > 0)
			{
				$arr_Results = $obj_TABLE_Metric->get_ResultSet();
				if (isset($arr_Results[0]))
				{
					$obj_TABLE_Metric->load_DataFromArray($arr_Results[0]);
					$obj_Date = new \DateTime();
					$obj_TABLE_Metric->set_datetime($obj_Date->format("Y-m-d H:i:s"));
				}
			}
			$obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->set_subcategory(\cTABLE_Metric::C_STR_SUBCATEGORY_MONTHLY);
			$obj_TABLE_Metric->set_group(\cTABLE_Metric::C_STR_GROUP_SOLR);
			$obj_TABLE_Metric->set_value($int_Count);

			$obj_TABLE_Metric->save_Data2DB();
		}

		private function store_Yearly($int_Count, $bool_Force = false)
		{
			$obj_StartTime = new \DateTime();
			$obj_StartTime->setTime(0, 0, 0);
			$obj_StartTime->setDate($obj_StartTime->format("Y"), 1, 1);

			$obj_EndTime = clone $obj_StartTime;
			$obj_EndTime->modify('+1 years');

			$obj_TABLE_Metric = new \cTABLE_Metric();

			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Category, \cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Subcategory, \cTABLE_Metric::C_STR_SUBCATEGORY_YEARLY);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_Group, \cTABLE_Metric::C_STR_GROUP_SOLR);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_StartTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_GREATER);
			$obj_TABLE_Metric->add_ColumnQuery($obj_TABLE_Metric->m_int_COL_DateTime, $obj_EndTime->format("Y-m-d H:i:s"), \cCondition::c_str_OPERATOR_LESS);

			if ($obj_TABLE_Metric->do_Search() > 0)
			{
				$arr_Results = $obj_TABLE_Metric->get_ResultSet();
				if (isset($arr_Results[0]))
				{
					$obj_TABLE_Metric->load_DataFromArray($arr_Results[0]);
					$obj_Date = new \DateTime();
					$obj_TABLE_Metric->set_datetime($obj_Date->format("Y-m-d H:i:s"));
				}
			}
			$obj_TABLE_Metric->set_category(\cTABLE_Metric::C_STR_CATEGORY_TOTAL_PAGES);
			$obj_TABLE_Metric->set_subcategory(\cTABLE_Metric::C_STR_SUBCATEGORY_YEARLY);
			$obj_TABLE_Metric->set_group(\cTABLE_Metric::C_STR_GROUP_SOLR);
			$obj_TABLE_Metric->set_value($int_Count);

			$obj_TABLE_Metric->save_Data2DB();

		}

		private function collect_Count()
		{
			try
			{
				$obj_Solr = new \cSolr(null, null, null);
				$this->output_Error("Using default solr instance -- count_SOLR_Docs");
			}
			catch (Exception $e)
			{
				$this->log_Error("The Solr Server is not responding.");
				return (false);
			}
			try
			{
				$obj_Response = $obj_Solr->search("*:*", 0, 0, array());
			}
			catch (Exception $e)
			{
				$this->log_Error("There has been an error Executeing your Query \n" . $e->getMessage());
				return (false);
			}

			if ($obj_Response->getHttpStatus() != 200)
			{
				$this->log_Error($obj_Response->getHttpStatusMessage());
				return (false);
			}

			return $obj_Response->response->numFound;
		}
	}
?>